import matplotlib.pyplot as plt
from TSP import TSP
from GA_2023_03_28 import GA
genset = 10000
evalset = 300000
m = 3
#case = 'gil262'
case = 'att48'
tsp_data = TSP(path=f'../../data/TSPLIB/{case}.tsp.txt')
localsearch = False


def drawpaint(tsp_data, optimizer):
    plt.subplot(1, 2, 1)
    bestlength = optimizer.caculatescore(optimizer.best)
    plt.plot(range(len(optimizer.evolve_list)), optimizer.evolve_list, label='optimal cost')
    plt.title(f'迭代{len(optimizer.evolve_list)}次的最优路径长度: {optimizer.evolve_list[-1]:.2f}', family='simsun')
    if optimizer.salesmannum > 1:
        plt.plot(range(len(optimizer.evolve_list)), optimizer.maxlengthevolve_list, label='optimal cost')
        plt.title(f'迭代{len(optimizer.evolve_list)}次的最长路径长度: {optimizer.maxlengthevolve_list[-1]:.2f}', family='simsun')
    plt.subplot(1, 2, 2)
    plt.title(f'迭代{len(optimizer.evolve_list)}次的最优路径长度: {bestlength:.2f}', family='simsun')
    optimizer.best.drawroute(tsp_data, plt, optimizer.salesmannum)


#optimizer = GAmultichromosome(tsp_data, crossrate=0.6, mutationrate=0.4, salesmannum=m, localsearch=localsearch)
optimizer = GA(tsp_data, crossrate=0.6, mutationrate=0.4, populationsize=100, crossovertype='EAX', localsearch=localsearch)
try:
    optimizer.optimize(evalset=evalset)
    # optimizer.optimize(genset=genset)
except KeyboardInterrupt:
    optimizer.LocalSearch(localsearch)
    
fig = plt.figure(figsize=(8, 3))
drawpaint(tsp_data, optimizer)
plt.savefig('sample.svg')
plt.show()











